package app.util.image.action.morph
{
	import app.util.*;
	import app.util.image.*;
	import app.util.image.action.*;
	import app.util.image.calc.*;
	
	import flash.display.*;

	public class DistanceTransformAction extends SEBasedAction
	{
		private var src:BinaryBitmapData;
		private var mid:BinaryBitmapData;
		private var ret:GrayBitmapData;		
		private var currentGray:Number = 3;
		
		public function DistanceTransformAction()
		{
			super(1);
		}
		
		public override function getDescription():String
		{
			return "Distance Transform";
		}
		
		protected override function getFinalBitmapData():BitmapData
		{
			return this.ret.toBitmapData();
		}
		
		protected override function processBitmapDataByStep():Number
		{
			if(this.ret == null)
			{
				se = MorphUtility.getDefaultSE();
				src = MorphUtility.autoThreshold(Mediator.getCurrentBitmapData());	
				ret = new GrayBitmapData(src.getWidth(), src.getHeight());
				mid = MorphUtility.doErosion(src, se);
			}
				
			if(!mid.isEmpty())
			{
				var diff:BinaryBitmapData = src.minus(mid);
				src = mid;
				ret.setGrayFromBinaryBitmap(diff, currentGray);
				mid = MorphUtility.doErosion(src, se);
				currentGray += 3;
				if(currentGray < 256)
				{
					return currentGray / 256;
				}
				else
				{
					return 0.95;
				}
			}			
			else
			{
				return 1.0;
			}
		}
	}
}